<?php

namespace HuoShu;

class Benefit{
    private $game_model,$agent_game_model;
    
    public function __construct() {
        $this->game_model=M('game');
        $this->agent_game_model=M('agent_game');
    }
    
    public function get_AgentApp_benefit_info($ag_id){
        
        $where=array("ag.id"=>$ag_id);				
		
		$info = $this->agent_game_model
                ->field("g.name,ag.app_id,ag.id,agr.*")
                ->alias("ag")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ag.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON agr.ag_id=ag.id")
                ->where($where)
                ->find();
        $this->type_txt_single($info);
        
        $this->benefit_info_output_item($info);
        return $info;
    }
    
    public function benefitUndefinedAppList(){
        $where="(gr.benefit_type IS NULL) AND (g.is_delete = 2) AND (g.status=2)";				
		
		$items = $this->game_model
                ->alias("g")
                ->where($where)
                ->join("LEFT JOIN ".C('DB_PREFIX')."game_rate gr ON gr.app_id = g.id")
                ->select();
        $this->type_txt($items);
        return $items;
    }
    public function benefitDefinedAppList(){
        $where="gr.benefit_type IS NOT NULL ";
        $appname = I("appname","");
        if(!empty($appname)){
            $where .= " and g.name = '".$appname."'";
        }
		$items = $this->game_model
                ->field("g.id,g.name,gr.*")
                ->alias("g")
                ->where($where)
                ->join("LEFT JOIN ".C('DB_PREFIX')."game_rate gr ON gr.app_id = g.id")
                ->order("gr.update_time desc")
                ->select();     
        $this->type_txt($items);
        return $items;
    }
    private function type_txt(&$items){
        foreach($items as $k => $item){
            $txt='';
            if($item['benefit_type']==1){
                $txt='折扣';
            }else if($item['benefit_type']==2){
                $txt='返利';
            }
            $items[$k]['benefit_type_txt']=$txt;
        }
    }
    private function type_txt_single(&$item){

            $txt='';
            if($item['benefit_type']==1){
                $txt='折扣';
            }else if($item['benefit_type']==2){
                $txt='返利';
            }
            $item['benefit_type_txt']=$txt;
        
    }
    public function app_benefit_info_exist($app_id){
        return M('game_rate')->where(array("app_id"=>$app_id))->find();
        
    }
    public function addAppBenefit($app_id,$data){
        if($this->app_benefit_info_exist($app_id)){
            $data['create_time']=time();
            $data['update_time']=time();
            M('game_rate')->where(array("app_id"=>$app_id))->save($data);
        }else{
            $data['app_id']=$app_id;
            $data['create_time']=time();
            $data['update_time']=time();  
            M('game_rate')->add($data);
        }
    }
    
    public function setAppBenefit($app_id,$data){
            $data['create_time']=time();
            $data['update_time']=time();
            M('game_rate')->where(array("app_id"=>$app_id))->save($data);
    }
    
    public function setAgentGameBenefit($ag_id,$data){
        $add_data=array();
        $add_data['agent_rate']=$data['agent_rate'];
        
        $add_data['create_time']=time();
        $add_data['update_time']=time();                
        
        M('agent_game_rate')->where(array("ag_id"=>$ag_id))->save($add_data);
    }
    
    
    public function rate_filter($name){
        if(isset($_POST[$name])&&is_numeric($_POST[$name]) && $_POST[$name]>0 && $_POST[$name]<1 ){
            return true;
        }
        return false;
    }
    
    
    public function rebate_filter($name,$field){
        
        if(!isset($_POST["$field"])){
            return " $name 不存在";
        }
        if(!is_numeric($_POST["$field"])){
            return "$name 不是数字";
        }
        if(!($_POST["$field"]<9)){
            return " $name 必须在0到9之间";
        }
        if(!($_POST["$field"]>0)){
            return "$name 必须在0到9之间";
        }
        return "ok";
    }
    public function benefilt_value_filter(){
        if(!$this->rate_filter("agent_rate")){
            return array("status"=>false,"msg"=>"渠道折扣错误");
        }
        return array("status"=>true);

                
    }
    
    public function getAgentInfo($agentId){
        $result=M('users')->where(array("id"=>$agentId))->find();
        $ownerid=$result['ownerid'];
        $result['belong_to_agent_id']=$ownerid;
        
        $owner_data=M('users')->where(array("id"=>$ownerid))->find();
        $result['belong_to_agent_name']=$owner_data['user_login'];
        
        return $result;
    }
    public function AgentGameList($start=0,$limit=0){
        $where="agr.benefit_type IS NOT NULL ";
        $appname = I("appname","");
        if(!empty($appname)){
            $where .= " and g.name = '".$appname."'";
        }
        $agent = I("agent","");
        if(!empty($agent)){
            $where .= " and u.user_login = '".$agent."'";
        }
        
        $items=  M('agent_game')
                ->field("g.name as game_name,ag.*,u.user_login as agent_name,agr.*")
                ->alias('ag')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ag.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ag.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON agr.ag_id=ag.id")
                ->where($where)
                ->order("ag.id desc")
                ->limit($start,$limit)
                ->select();
        $this->type_txt($items);
        
        
        foreach($items as $k=>$v){
            $data=$this->getAgentInfo($v['agent_id']);
            $items[$k]['belong_to_agent_id']=$data['belong_to_agent_id'];
            $items[$k]['belong_to_agent_name']=$data['belong_to_agent_name'];
        }
        
        $this->benefit_info_output($items);
        
        return $items;
    }
    
    public function AgentGameList_single($agent_id,$start=0,$limit=0){
        $hs_agent_obj=new \HuoShu\Agent($agent_id);
        $ids=$hs_agent_obj->getMySubAgentsIds();
        array_push($ids,$agent_id);
        $ids_txt=join(",",$ids);
        $where=array();
        $where['_string']="ag.agent_id IN ($ids_txt)";
        $where['g.is_delete']=2;
        $items=  M('agent_game')
                ->field("g.name as game_name,ag.id,u.user_login as agent_name,u.user_type,agr.*")
                ->alias('ag')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ag.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ag.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON agr.ag_id=ag.id")
                ->where($where)
                ->order("ag.id desc")
                ->limit($start,$limit)
                ->select();
        $this->type_txt($items);
        foreach($items as $k=>$v){
            $data=$this->getAgentInfo($v['agent_id']);
            $items[$k]['belong_to_agent_id']=$data['belong_to_agent_id'];
            $items[$k]['belong_to_agent_name']=$data['belong_to_agent_name'];
        }
        $this->benefit_info_output($items);
        
        return $items;
    }
    
    
    public function get_app_default_agent_rate($app_id){
        $default_agent_rate=M('game_rate')->where(array("app_id"=>$app_id))->getField("agent_rate");
        
        return $default_agent_rate;
    }
    
    public function get_app_default_agentrate_by_agid($ag_id){
        
        $data=M('agent_game')->where(array("id"=>$ag_id))->find();
        $app_id=$data['app_id'];
        return M('game_rate')->where(array("app_id"=>$app_id))->getField("agent_rate");
    }
    
    public function set_agent_game_agent_rate($agid,$value){        
        return M('agent_game_rate')->where(array("ag_id"=>$agid))->setField("agent_rate",$value);
    }
    public function get_agent_game_agent_rate($agid){        
        return M('agent_game_rate')->where(array("ag_id"=>$agid))->getField("agent_rate");
    }
    public function get_agent_game_agent_rate_V2($agent_id,$app_id){        
        return M('agent_game_rate')->where(array("agent_id"=>$agent_id,"app_id"=>$app_id))->getField("agent_rate");
    }
    public function set_app_agent_rate($app_id,$value){
        return M('game_rate')->where(array("app_id"=>$app_id))->setField("agent_rate",$value);
    }
    
    public function set_app_agentgame_all_agentrate($app_id,$value){
        return M('agent_game_rate')->where(array("app_id"=>$app_id))->setField("agent_rate",$value);
    }
    
    public function set_app_agentgame_agentrate($ag_id,$value){
        return M('agent_game_rate')->where(array("id"=>$ag_id))->setField("agent_rate",$value);
    }
    
    public function get_agentgame_agentrate_info($agent_id,$app_id){
        $data= M('agent_game_rate')->where(array("agent_id"=>$agent_id,"app_id"=>$app_id))->find();
        return $data;
    }
    
    public function get_app_benefit_info($app_id){
        $result= M('game_rate')->where(array("app_id"=>$app_id))->find();
        $this->benefit_info_output_item($result);
        return $result;
    }
    
    public function benefit_info_output(&$data){
        foreach($data as $k=>$v){
            if($v['benefit_type']=="1"){
                $data[$k]['benefit_first']=$v['first_mem_rate'];
                $data[$k]['benefit_refill']=$v['mem_rate'];
            }else if($v['benefit_type']=="2"){
                $data[$k]['benefit_first']=$v['first_mem_rebate'];
                $data[$k]['benefit_refill']=$v['mem_rebate'];
            }
        }
        
    }
    
     public function benefit_info_output_item(&$data){

        if($data['benefit_type']=="1"){
            $data['benefit_first']=$data['first_mem_rate'];
            $data['benefit_refill']=$data['mem_rate'];
        }else if($data['benefit_type']=="2"){
            $data['benefit_first']=$data['first_mem_rebate'];
            $data['benefit_refill']=$data['mem_rebate'];
        }
    }
    
    public function set_agentgame_benefit_by_app_benefit($ag_id,$app_id,$agent_id){
        $game_benefit_data=$this->get_app_benefit_info($app_id);
        $data['benefit_type']=$game_benefit_data['benefit_type'];
        $data['agent_rate']=$game_benefit_data['agent_rate'];
        $data['create_time']=time();
        $data['update_time']=time();
        $exist=M('agent_game_rate')->where(array("ag_id"=>$ag_id))->find();        
        
        if(!$exist){
            $data['ag_id']=$ag_id;
            $data['app_id']=$app_id;
            $data['agent_id']=$agent_id;           
            M('agent_game_rate')->add($data);
        }else{
            M('agent_game_rate')->where(array("ag_id"=>$ag_id))->save($data);
        }
    }
    
    public function set_app_benefit_info($app_id,$data){
        M('game_rate')->where(array("app_id"=>$app_id))->save($data);
    }
    
    public function set_agentgame_all_benefit_type($app_id,$value){
        M('agent_game_rate')->where(array("app_id"=>$app_id))->setField("benefit_type",$value);
    }      
}

